﻿using Avalonia;
using FantomNext.Controls;
using FantomNext.Factories;
using FantomNext.Helpers;
using FantomNext.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FantomNext.Presenters
{
    public partial class ScenePresenter
    {
        public ShapePresenter CreateRect(float x, float y, float width, float height)
        {
            ShapePresenter shapePresenter = CreateShapePresenter(x, y, width, height);

            AddShapePresenter(shapePresenter);

            return shapePresenter;
        }


        public ShapePresenter CreateTextBox(float x, float y, float width, float height)
        {
            var shape = new Shape() { Id = Guid.NewGuid().ToString(), Position = new Vector2(x, y), Size = new Vector2(width, height), ShapeType = ShapeType.Text };
            var view = new FantomTextBox(shape.Id);
            var shapePresenter = new ShapePresenter(view, shape);

            shapePresenter.Load();

            _view.PART_FantomCanvas.Children.Add(view);

            _shapes.Add(shapePresenter);

            return shapePresenter;
        }

        private ShapePresenter CreateShapePresenter(Rect rect)
        {
            return CreateShapePresenter(rect.X, rect.Y, rect.Width, rect.Height);
        }

        private ShapePresenter CreateShapePresenter(double x, double y, double width, double height)
        {
            var shapeFactory = IocHelper.GetService<ShapeFactory>();
            //var fantomShapeFactory = IocHelper.GetService<FantomShapeFactory>();
            var shapePresenterFactory = IocHelper.GetService<ShapePresenterFactory>();
            var shape = shapeFactory.Create(ShapeFactory.FactoryType.PresetShape);
            shape.Position = new Vector2(x, y);
            shape.Size = new Vector2(width, height);
            var shapePresenter = shapePresenterFactory.Create(shape);

            ConfigShapePresenterView(shapePresenter.View);

            return shapePresenter;
        }

  

        /// <summary>
        /// 添加图形。
        /// </summary>
        /// <param name="shapePresenter"></param>
        private void AddShapePresenter(ShapePresenter shapePresenter)
        {
            _shapes.Add(shapePresenter);
            _view.PART_FantomCanvas.Children.Add(shapePresenter.View);
        }

    }
}
